package com.songcha.sakura.mvvm.home.schedule

import android.annotation.SuppressLint
import android.os.Bundle
import com.songcha.library_base.mvvm.base.BaseMvvmFragment
import com.songcha.sakura.R
import com.songcha.sakura.bean.ComicScheduleListBean
import com.songcha.sakura.databinding.FragmentHomeScheduleBinding
import com.songcha.sakura.ui.adapter.HomeScheduleAdapter
import com.songcha.library_common.util.TimeUtil
import com.songcha.sakura.ui.base.BaseComicMvvmFragment
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import java.util.concurrent.TimeUnit

class HomeScheduleFragment: BaseComicMvvmFragment<HomeScheduleViewModel, FragmentHomeScheduleBinding>() {
    companion object{
        val TAG:String=HomeScheduleFragment::class.java.simpleName
    }
    override fun getLayoutId(): Int {
        return R.layout.fragment_home_schedule
    }

    override fun isNeedShowLoad(): Boolean {
        return true
    }

    override fun initView(savedInstanceState: Bundle?){

        onLoading()
        viewModel.getScheduleList()

        viewModel.comicList.observe(viewLifecycleOwner){
            initData(it)
        }
    }



    @SuppressLint("CheckResult")
    private fun initData(bean: ComicScheduleListBean){
        val vp=databinding.vpSchedule
        vp.adapter=HomeScheduleAdapter(childFragmentManager,bean.data)
        databinding.ctlSchedule.setViewPager(vp)

        var day= TimeUtil.getDayInWeek()
        if(day==1){
            day=6
        }else{
            day-=2
        }

        Observable.timer(2000,TimeUnit.MILLISECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe{
                databinding.ctlSchedule.selectTab(day)
            }

    }


    override fun onDestroyView() {
        super.onDestroyView()
        //_binding = null
    }




}